﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class User_UserNote_MsgToShow : System.Web.UI.Page {
    private string sendUserLoginName;
    private int mid;

    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            if(Request["mid"] != null) {
                mid = int.Parse(Request["mid"]);
                ViewState["mid"] = mid;
            }
            if(mid == 0) {
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "", "alert('没有消息显示！')", true);
            } else {
                this.findMsgDataBind(mid);
            }
        }
    }

    //根据消息编号查看消息内容绑定
    protected void findMsgDataBind(int mid) {
        SectionEmpMsgtbService sectionEmpMsgtbService = new SectionEmpMsgtbService();
        SectionEmpMsgtb sectionEmpMsg = sectionEmpMsgtbService.findByMsgId(mid, "incept");
        if(sectionEmpMsg != null && sectionEmpMsg.SendUser != null)
        {
            sectionEmpMsgtbService.updateMsgState(mid, "incept");
            this.lblSendDateTime.Text = sectionEmpMsg.SendDateTime.ToString();
            this.lblSendUserName.Text = sectionEmpMsg.SendUser.UserName;
            this.sendUserLoginName = sectionEmpMsg.SendUser.UserName;
            this.txtMsgContent.Text = sectionEmpMsg.MsgContent;
        }
        else
        {
            User curLoginUser = (User) Session["user"];
            if(ViewState["mid"] != null)
            {
                mid = (int) ViewState["mid"];
            }
            MessageAwoke.Instance().RemoveValueFromKeyForShortMsg(curLoginUser.LoginName, mid);
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('消息不存在或者已被删除！');window.close();", true);
        }
    }

    //protected void Button1_Click(object sender, EventArgs e) {
    //    this.txtMsgContent.Visible = false;
    //    this.txtSendMsg.Visible = true;
    //    this.Button1.Enabled = false;
    //    this.btnSendMsg.Enabled = true;
    //    User curLoginUser = (User) Session["user"];
    //    if(ViewState["mid"] != null) {
    //        mid = (int) ViewState["mid"];
    //    }
    //    MessageAwoke.Instance().RemoveValueFromKeyForShortMsg(curLoginUser.LoginName, mid);
    //}

    //protected void btnSendMsg_Click(object sender, EventArgs e) {
    //    SectionEmpMsgtbService sectionEmpMsgtbService = new SectionEmpMsgtbService();
    //    User curLoginUser = (User) Session["user"];
    //    int msg_id = sectionEmpMsgtbService.saveSectionEmpMsg(this.sendUserLoginName, curLoginUser.LoginName, this.txtSendMsg.Text, DateTime.Now);
    //    MessageAwoke.Instance().AddToShortMsg(sendUserLoginName, msg_id);
    //    //Application[sendUserLoginName] = curLoginUser.LoginName;
    //}

    protected void Button2_Click(object sender, EventArgs e) {
        User curLoginUser = (User) Session["user"];
        if(ViewState["mid"] != null) {
            mid = (int) ViewState["mid"];
        }
        MessageAwoke.Instance().RemoveValueFromKeyForShortMsg(curLoginUser.LoginName, mid);
        ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "window.history.go(-1);", true);
    }
}
